package Object.net.tjdz.view;

import Object.net.tjdz.model.Student;
import Object.net.tjdz.service.StudentService;
import java.util.List;
import java.util.Scanner;

public class StudentUI {
    private final StudentService service;
    private final Scanner scanner;

    public StudentUI(StudentService service) {
        this.service = service;
        this.scanner = new Scanner(System.in);
    }

    public void showMenu() {
        while (true) {
            System.out.println("\n--- 学生信息管理 ---");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询所有学生");
            System.out.println("5. 按学号查询");
            System.out.println("0. 返回主菜单");
            System.out.print("请选择：");

            int choice = getIntInput();

            switch (choice) {
                case 1 -> addStudent();
                case 2 -> deleteStudent();
                case 3 -> updateStudent();
                case 4 -> showAllStudents();
                case 5 -> findStudentById();
                case 0 -> { return; }
                default -> System.out.println("无效输入，请重新选择！");
            }
        }
    }

    private void addStudent() {
        System.out.println("\n--- 添加学生 ---");
        System.out.print("学号：");
        String id = scanner.nextLine();

        if (service.getById(id) != null) {
            System.out.println("该学号已存在！");
            return;
        }

        System.out.print("姓名：");
        String name = scanner.nextLine();

        System.out.print("年龄：");
        int age = getIntInput();
        scanner.nextLine(); // 清除换行符

        System.out.print("性别：");
        String gender = scanner.nextLine();

        Student student = new Student(id, name, age, gender);
        if (service.add(student)) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private void deleteStudent() {
        System.out.println("\n--- 删除学生 ---");
        System.out.print("输入要删除的学号：");
        String id = scanner.nextLine();

        if (service.delete(id)) {
            System.out.println("删除成功！");
        } else {
            System.out.println("学号不存在！");
        }
    }

    private void updateStudent() {
        System.out.println("\n--- 修改学生 ---");
        System.out.print("输入要修改的学号：");
        String id = scanner.nextLine();

        Student student = service.getById(id);
        if (student == null) {
            System.out.println("学号不存在！");
            return;
        }

        System.out.println("当前信息：" + student);
        System.out.print("新姓名（留空不变）：");
        String name = scanner.nextLine();
        if (!name.isEmpty()) student.setName(name);

        System.out.print("新年龄（留空不变）：");
        String ageInput = scanner.nextLine();
        if (!ageInput.isEmpty()) student.setAge(Integer.parseInt(ageInput));

        System.out.print("新性别（留空不变）：");
        String gender = scanner.nextLine();
        if (!gender.isEmpty()) student.setGender(gender);

        if (service.update(id, student)) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    private void showAllStudents() {
        System.out.println("\n--- 所有学生列表 ---");
        List<Student> students = service.getAll();

        if (students.isEmpty()) {
            System.out.println("没有学生记录");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t性别");
        students.forEach(s -> System.out.println(
                s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getGender()));
    }

    private void findStudentById() {
        System.out.println("\n--- 按学号查询 ---");
        System.out.print("输入学号：");
        String id = scanner.nextLine();

        Student student = service.getById(id);
        if (student != null) {
            System.out.println("查询结果：");
            System.out.println(student);
        } else {
            System.out.println("学号不存在！");
        }
    }

    private int getIntInput() {
        while (!scanner.hasNextInt()) {
            System.out.print("请输入数字：");
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine(); // 清除换行符
        return input;
    }
}